﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class VehicleMissionStatusRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<VehicleMissionStatus> GetAllData()
        {
            return from VehicleMissionStatus in db.VehicleMissionStatuses
                   orderby VehicleMissionStatus.VehicleMissionStatusCode descending
                   select VehicleMissionStatus;
        }


        public IQueryable<VehicleMissionStatus> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.VehicleMissionStatuses.Where(p => p.VehicleMissionStatusCode.Contains(searchString));
            if (searchField == "VehicleMissionStatusDescription")
                ps = db.VehicleMissionStatuses.Where(p => p.VehicleMissionStatusDescription.Contains(searchString));
            if (searchField == "VehicleMissionStatusCode")
                ps = db.VehicleMissionStatuses.Where(p => p.VehicleMissionStatusCode.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<VehicleMissionStatus> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public VehicleMissionStatus GetByID(string id)
        {
            return db.VehicleMissionStatuses.SingleOrDefault(VehicleMissionStatus => VehicleMissionStatus.VehicleMissionStatusCode == id);
        }

        //Inset
        public void Add(VehicleMissionStatus VehicleMissionStatus)
        {
            //if (VehicleMissionStatus.StatusID == 0 )
            //    VehicleMissionStatus.StatusID = 1;
            VehicleMissionStatus.InsertedUser = HttpContext.Current.User.Identity.Name;
            VehicleMissionStatus.InsertedDate = DateTime.Now;
            VehicleMissionStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            VehicleMissionStatus.UpdatedDate = DateTime.Now;
            VehicleMissionStatus.DataVersion = 1;

            db.VehicleMissionStatuses.AddObject(VehicleMissionStatus);
        }

        //Update
        public void Update(VehicleMissionStatus VehicleMissionStatus)
        {

            VehicleMissionStatus originalVehicleMissionStatus = GetByID(VehicleMissionStatus.VehicleMissionStatusCode);

            VehicleMissionStatus.InsertedUser = originalVehicleMissionStatus.InsertedUser;
            VehicleMissionStatus.InsertedDate = originalVehicleMissionStatus.InsertedDate;
            VehicleMissionStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            VehicleMissionStatus.UpdatedDate = DateTime.Now;
            VehicleMissionStatus.DataVersion = ++originalVehicleMissionStatus.DataVersion;
  
            db.VehicleMissionStatuses.ApplyCurrentValues(VehicleMissionStatus);
        }

        //Delete
        public void Delete(VehicleMissionStatus VehicleMissionStatus)
        {
            db.VehicleMissionStatuses.DeleteObject(VehicleMissionStatus);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}